<?php
namespace app\index\controller;
use app\index\model\ActionRole;
use app\index\model\NoticeProduct;
use app\index\model\Notice;
use app\index\model\Product;
use think\Db;
use think\response\Json;

class NoticeController extends BaseController
{
    public function getNotice()
    {
        $page = input('post.pageIndex', 1);
        $size = input('post.pageSize', 10);
        $post = input('post.', [], 'trim');
        $search_param = [
            'search_key' => $post['search_key'] ?? null,
            'search_value' => $post['search_value'] ?? null,
            'type' => $post['type'] ?? null,
            'notice_time' => $post['notice_time'] ?? null,
        ];
        $page_param = [
            'page_num' => $page,
            'page_size' => $size,
        ];
        ['status'=>$status,'result'=>$result]=Notice::getNotice($search_param,$page_param);
        return json(['code'=>0,'data'=>$result['data'],'total'=>$result['total']]);
    }
    public function getNoticeParam()
    {
        $notice= new Notice();
        $marks=Db::name('mark')->where('del_flag',0)->column('mark_text','id');
        return json(['code'=>0,'type_list'=>$notice->type,'marks'=>$marks]);
    }

    public function getNoticeTitleList(){
        ['action_role'=>$action_role,'group'=>$search_group,'search_user'=>$search_user]=ActionRole::checkUserActionRole('notice_list');

        if($action_role['notice_list']===0){
            return json(['code'=>0,'data'=>[]]);
        }
        ['status'=>$status,'result'=>$result]=Notice::getNoticeTitleList();
        return json(['code'=>0,'data'=>$result['data']]);
    }

    public function addNotice(){
        $data=input('post.');
        $insert_data=[
            'title'=>$data['title'],
            'type'=>$data['type'],
            'content'=>$data['content'],
            'notice_time'=>$data['notice_time'],
            'create_user'=>$this->user_session['id']
        ];
        $product=[];
        if($data['type']===1){
            $product=$data['product'];
        }
        ['status'=>$status,'result'=>$result]=Notice::addNotice($insert_data,$product);
        if($status===false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }

    public function getNoticeById(){
        $id=input('post.id');
        if(empty($id)){
           return json(['code'=>1,'msg'=>'请输入id']);
        }
        ['status'=>$status,'result'=>$result]=Notice::getNoticeById($id);
        if($status){
            return json(['code'=>0,'data'=>$result]);
        }
        return json(['code'=>1,'msg'=>$result]);
    }

    public function updateNotice(){
        $data=input('post.');
        $update_data=[
            'id'=>$data['id'],
            'title'=>$data['title'],
            'type'=>$data['type'],
            'content'=>$data['content'],
            'notice_time'=>$data['notice_time'],
        ];
        $product=[];
        if($data['type']===1){
            $product=$data['product'];
        }
        ['status'=>$status,'result'=>$result]=Notice::updateNotice($update_data,$product);
        if($status===false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }

    public function getNoticeDetailById(){
        $id=input('post.id');
        if(empty($id)){
            return json(['code'=>1,'msg'=>'请输入id']);
        }
        ['status'=>$status,'result'=>$result]=Notice::getNoticeDetailById($id);
        if($status){
            return json(['code'=>0,'data'=>$result]);
        }
        return json(['code'=>1,'msg'=>$result]);
    }

    public function getProduct()
    {
        $id=input('post.id');
        ['status'=>$status,'result'=>$result]=Product::getProduct($id);
        return json(['code'=>0,'data'=>$result]);
    }
}